const isProduction = process.env.NODE_ENV === 'production' // 是否在正式环境
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const ScriptSetup = require('unplugin-vue2-script-setup/webpack').default
const productionGzipExtensions = ['js', 'css']
const path = require('path')
function resolve (dir) {
    return path.join(__dirname, dir)
}
module.exports = {
    publicPath: process.env.VUE_APP_PUBLIC_PATH,
    productionSourceMap: false,
    css: {
        loaderOptions: {
            sass: {
                prependData: '@import "~yaoxfly-ui/src/style/admin/base.scss";'
            }
        },
        extract: true,
        sourceMap: false
    },

    chainWebpack: config => {
        config.plugin('html')
            .tap(args => {
                args[0].title = '基础框架'
                return args
            })
    },

    configureWebpack: config => {
        isProduction && config.plugins.push(
            new CompressionWebpackPlugin({
                algorithm: 'gzip',
                test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                threshold: 10240,
                minRatio: 0.8
            })
        )
        config.resolve.alias['@asset'] = resolve('src/assets')
        config.resolve.alias['@'] = resolve('src')
        config.resolve.alias._c = resolve('src/components')
        config.plugins.push(ScriptSetup())
    },
    devServer: {
        open: false,
        disableHostCheck: true, // 防止 invalid host header
        compress: true,
        overlay: {
            // 出现编译器错误或警告时，在浏览器中显示全屏覆盖层
            warnings: true,
            errors: true
        },
        // historyApiFallback: true,
        proxy: {
            '/api': {
                target: 'http://localhost:8080', // 后端接口地址
                changeOrigin: true, // 是否启动代理(是否跨域)
                secure: false, // 如果是https接口，需要配置这个参数
                ws: true, // 是否启用websockets;
                pathRewrite: {
                    '^/api': ''
                }
            }
            // '/upms-center': {
            //     target: 'http://192.168.180.106:47080/kong/kong-gateway/upms-center', // 后端接口地址
            //     changeOrigin: true, // 是否启动代理(是否跨域)
            //     secure: false, // 如果是https接口，需要配置这个参数
            //     ws: true, // 是否启用websockets;
            //     pathRewrite: {
            //         '^/upms-center': ''
            //     }
            // }
        }
    },
    transpileDependencies: ['yaoxfly-ui', 'element-ui']
}
